package com.zzk.lanouhn.myapplication1.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RadioButton;

import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.adapters.MainViewPagerAdapter;
import com.zzk.lanouhn.myapplication1.fragments.FindFragment;
import com.zzk.lanouhn.myapplication1.fragments.InvestFragment;
import com.zzk.lanouhn.myapplication1.fragments.MessageFragment;
import com.zzk.lanouhn.myapplication1.fragments.MineFragment;
import com.zzk.lanouhn.myapplication1.fragments.NewsFragment;
import com.zzk.lanouhn.myapplication1.modles.MessageEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private ViewPager vp;
    private RadioButton btn_news, btn_invest, btn_find, btn_message, btn_mine;
    private MainViewPagerAdapter mainAdapter;
    private List<Fragment> lists;
    private NewsFragment newsFragment;
    private InvestFragment investFragment;
    private FindFragment findFragment;
    private MessageFragment messageFragment;
    private MineFragment mineFragment;
    private DrawerLayout drawer;

    private SharedPreferences sp;
    private String donglu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);

        initView();
        initData();
        initEvent();

    }


    private void initView() {
        vp = (ViewPager) findViewById(R.id.vp_main);
        btn_news = (RadioButton) findViewById(R.id.btn_news_main);
        btn_invest = (RadioButton) findViewById(R.id.btn_invest_main);
        btn_find = (RadioButton) findViewById(R.id.btn_find_main);
        btn_message = (RadioButton) findViewById(R.id.btn_message_main);
        btn_mine = (RadioButton) findViewById(R.id.btn_mine_main);
        drawer = (DrawerLayout) findViewById(R.id.drawerlayout_new);
        sp = getSharedPreferences("login", MODE_PRIVATE);
        donglu = sp.getString("shifou", "false");
    }

    private void initData() {
        lists = new ArrayList<>();
        newsFragment = new NewsFragment();
        lists.add(newsFragment);
        investFragment = new InvestFragment();
        lists.add(investFragment);
        findFragment = new FindFragment();
        lists.add(findFragment);
        messageFragment = new MessageFragment();
        lists.add(messageFragment);
        mineFragment = new MineFragment();
        lists.add(mineFragment);

        mainAdapter = new MainViewPagerAdapter(getSupportFragmentManager(), lists);
        vp.setAdapter(mainAdapter);
        vp.setOffscreenPageLimit(4);
    }

    private void initEvent() {
        btn_news.setOnClickListener(this);
        btn_invest.setOnClickListener(this);
        btn_find.setOnClickListener(this);
        btn_message.setOnClickListener(this);
        btn_mine.setOnClickListener(this);
        //是否登录
        if (donglu.equals("false")) {
            vp.setCurrentItem(0);
        } else {
            vp.setCurrentItem(4);
        }

        btn_news.setChecked(true);
        vp.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                vp.getParent().requestDisallowInterceptTouchEvent(true);
                switch (position) {
                    case 0:
                        btn_news.setChecked(true);
                        break;
                    case 1:
                        btn_invest.setChecked(true);
                        break;
                    case 2:
                        btn_find.setChecked(true);
                        break;
                    case 3:
                        btn_message.setChecked(true);
                        break;
                    case 4:
                        btn_mine.setChecked(true);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_news_main:
                vp.setCurrentItem(0, false);
                break;
            case R.id.btn_invest_main:
                vp.setCurrentItem(1, false);
                break;
            case R.id.btn_find_main:
                vp.setCurrentItem(2, false);
                break;
            case R.id.btn_message_main:
                vp.setCurrentItem(3, false);
                break;
            case R.id.btn_mine_main:
                vp.setCurrentItem(4, false);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void click(MessageEvent event2) {
        int number = event2.fragmentId;
//        if (number == R.id.icon_title_news) {
//            drawer.openDrawer(Gravity.LEFT);
//            drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.LEFT);
//        }
        switch (number) {
            case R.id.icon_title_news:
                drawer.openDrawer(Gravity.LEFT);
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.LEFT);
                break;
            case R.id.iv_back_menu:
                drawer.closeDrawer(Gravity.LEFT);
                drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.LEFT);
                break;
            default:

                break;
        }
    }
}
